	//
//  HomeAddGoodsTableViewCell.m
//  ZJControlsClass
//
//  Created by mac on 2024/12/3.
//

#import "HomeAddGoodsTableViewCell.h"
#import "HomeAddGoodsNextTableViewCell.h"

@interface HomeAddGoodsTableViewCell ()<UITableViewDelegate,UITableViewDataSource>{
}



@property (nonatomic,strong) UITableView * tableView;
@property (nonatomic,strong) NSMutableArray * dataSource;






@end



@implementation HomeAddGoodsTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    
    if (self){
        
        [self configUI];
        
    }
    
    return self;
}



-(void)configUI{
    
    [self.contentView addSubview:self.tableView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(self.contentView);
        make.left.equalTo(self.contentView).with.offset(ALIGN_WIDTH_PT(15));
        make.right.equalTo(self.contentView).with.offset(-ALIGN_WIDTH_PT(15));
    }];

    
}


-(void)setModel:(HomeUnitModel *)model{
    _model = model;
    [self.dataSource removeAllObjects];
    [self.dataSource addObject:[propssModel mj_objectWithKeyValues:model.props]];
    [self.tableView reloadData];
    
    
    
}




#pragma mark -- 点击事件
-(void)respondsTo_addCell{
    propssModel * props = [propssModel new];
    props.name = self.model.props.title;
    props.spec = @"";
    props.num = @"0";
    
    [self.dataSource addObject:props];
    
    NSInteger hei = ALIGN_HEIGHT_PT(90) * self.dataSource.count + ALIGN_HEIGHT_PT(70);
    
    if(self.block) {
        self.block(hei);
    }
    
    [self.tableView reloadData];
}




#pragma mark -- UITableViewDelegate,UITableViewDataSource

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataSource.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HomeAddGoodsNextTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"HomeAddGoodsNextTableViewCell"];
    if (!cell){
        cell = [[HomeAddGoodsNextTableViewCell alloc]initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"HomeAddGoodsNextTableViewCell"];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if(indexPath.row < self.dataSource.count) {
        propssModel * model = self.dataSource[indexPath.row];
        cell.model = model;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
   
    if (editingStyle == UITableViewCellEditingStyleDelete){
        // 删除数据
        if (self.dataSource.count == 1){
            return;
        }else{

            HomeUnitModel * model = self.dataSource[indexPath.row];
            [self.dataSource removeObject:model];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:(UITableViewRowAnimationLeft)];
            NSInteger hei = (self.dataSource.count * ALIGN_HEIGHT_PT(180)) - ALIGN_HEIGHT_PT(180);
            _block(hei);
        }
    }
    
    
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ALIGN_HEIGHT_PT(30))];
    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - ALIGN_WIDTH_PT(30), ALIGN_HEIGHT_PT(30))];
    label.text = self.model.title;
    label.textAlignment = NSTextAlignmentLeft;
    label.font = FONT14;
    label.textColor = [UIColor blackColor];
    [headView addSubview:label];
    
    
    return headView;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView * footView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, ALIGN_HEIGHT_PT(40))];
    UIButton * addBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, ALIGN_HEIGHT_PT(5), ALIGN_WIDTH_PT(100) , ALIGN_HEIGHT_PT(30))];
    [addBtn setTitle:[NSString stringWithFormat:@"新增%@",self.model.props.title] forState:(UIControlStateNormal)];
    addBtn.titleLabel.font = FONT14;
    addBtn.backgroundColor = COLOR_RGB(50, 113, 237);
    [addBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    addBtn.layer.cornerRadius = 5;
    addBtn.layer.masksToBounds = YES;
    [addBtn addTarget:self action:@selector(respondsTo_addCell) forControlEvents:(UIControlEventTouchUpInside)];
    [footView addSubview:addBtn];
    
    return footView;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ALIGN_HEIGHT_PT(90);
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return ALIGN_HEIGHT_PT(30);
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return ALIGN_HEIGHT_PT(40);
}


#pragma mark -- 懒加载

-(UITableView *)tableView{
    if (!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:(UITableViewStylePlain)];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = YES;
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
    }
    return _tableView;
}

-(NSMutableArray *)dataSource{
    if (!_dataSource){
        _dataSource = [NSMutableArray array];
    }
    return _dataSource;
}




@end
